extends Control

@onready var createRoomPanel = $CreateRoomPanel
@onready var roomDescEdit:TextEdit = $CreateRoomPanel/CreateRoomCenterPanel/TextEdit
@onready var roomItemContainer = $RoomListContainer/VBoxContainer
@onready var returnButton = $BottomButtons/ReturnButton

func _ready():
	GlobalSignals.NM_CREATEROOM_RESULT.connect(NM_CREATEROOM_RESULT_handler)
	GlobalSignals.NM_GET_ROOMLIST_RESULT.connect(NM_GET_ROOMLIST_RESULT_handler)
	GlobalSignals.NM_JOINROOM_RESULT.connect(NM_JOINROOM_RESULT_handler)
	GlobalSignals.NM_ROOM_DESTROYED.connect(NM_ROOM_DESTROYED_handler)
	GlobalSignals.NM_ROOM_CHANGED.connect(NM_ROOM_CHANGED_handler)
	GlobalSignals.NM_LOGOUT_RESULT.connect(NM_LOGOUT_RESULT_handler)
	
	clearRoomList()
	getRoomList()
	

func _exit_tree():
	GlobalSignals.NM_CREATEROOM_RESULT.disconnect(NM_CREATEROOM_RESULT_handler)
	GlobalSignals.NM_GET_ROOMLIST_RESULT.disconnect(NM_GET_ROOMLIST_RESULT_handler)
	GlobalSignals.NM_JOINROOM_RESULT.disconnect(NM_JOINROOM_RESULT_handler)
	GlobalSignals.NM_ROOM_DESTROYED.disconnect(NM_ROOM_DESTROYED_handler)
	GlobalSignals.NM_ROOM_CHANGED.disconnect(NM_ROOM_CHANGED_handler)
	GlobalSignals.NM_LOGOUT_RESULT.disconnect(NM_LOGOUT_RESULT_handler)


func showCreateRoom():
	createRoomPanel.visible = true
	
	
func hideCreateRoom():
	createRoomPanel.visible = false


func _on_create_room_button_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	showCreateRoom()


func _on_cancel_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	hideCreateRoom()


func _on_confirm_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	var data = {
		"roomDesc": roomDescEdit.text
	}
	NetClient.sendMessage(NetMessages.NM_CREATEROOM, data)
	
	
func NM_CREATEROOM_RESULT_handler(data):
	Global.currentRoom = data["room"]
	Global.changeScene("Room")
	
	
func NM_GET_ROOMLIST_RESULT_handler(data):
	print("NM_GET_ROOMLIST_RESULT_handler", data)
	var rooms = data["rooms"]
	for room in rooms:
		var roomItemNode:RoomItem = Global.createSceneInst("RoomItem") as RoomItem
		roomItemContainer.add_child(roomItemNode)
		roomItemNode.init(room)
		roomItemNode.onJoinRoomButtonPressed.connect(onJoinRoomButtonPressedHandler)


func NM_JOINROOM_RESULT_handler(data):
	Global.currentRoom = data["room"]
	Global.changeScene("Room")
	
	
func NM_ROOM_DESTROYED_handler(data):
	clearRoomList()
	getRoomList()
	

func NM_ROOM_CHANGED_handler(data):
	clearRoomList()
	getRoomList()
	

func NM_LOGOUT_RESULT_handler(data):
	Global.changeScene("Title")
	
	
func clearRoomList():
	for child in roomItemContainer.get_children():
		child.queue_free()
		

func getRoomList():
	var data = {
		
	}
	NetClient.sendMessage(NetMessages.NM_GET_ROOMLIST, data)


func onJoinRoomButtonPressedHandler(room):
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	
	var data = {
		"roomId" : room["roomId"]
	}

	NetClient.sendMessage(NetMessages.NM_JOINROOM, data)


func _on_return_button_pressed():
	GlobalSignals.PLAY_SOUND.emit(SoundManager.CLICK)
	NetClient.sendMessage(NetMessages.NM_LOGOUT, {})
